class AppBootHook {
    constructor(app) {
        this.app = app;
    }

    async didLoad() {
        this.app.model.sync();
        // 所有的模型已经加载完毕
        if (this.app.model) {
            this.defineModelAssociations();
        }
    }

    defineModelAssociations() {
        const User = this.app.model.User;
        const Follow = this.app.model.Follow;
        const Conversation = this.app.model.Conversation;
        const Post = this.app.model.Post;
        const Comment = this.app.model.Comment;
        const Circle = this.app.model.Circle;
        const UserCircle = this.app.model.UserCircle;
        const CirclePost = this.app.model.CirclePost;
        const CirclePostComment = this.app.model.CirclePostComment;
        const Notification = this.app.model.Notification;
        const Message = this.app.model.Message;
        const Job = this.app.model.Job;
        const Resume = this.app.model.Resume;
        const JobApplication = this.app.model.JobApplication;
        const LostAndFound = this.app.model.LostAndFound;
        const Good = this.app.model.Good;
        const Order = this.app.model.Order;
        const Admin = this.app.model.Admin;
        const Announcement = this.app.model.Announcement;
        const Banner = this.app.model.Banner;
        const Alumni = this.app.model.Alumni;

        // User Follow (用户关注关系)
        User.hasMany(Follow, {
            foreignKey: 'user_id',
            as: 'following',
            sourceKey: 'user_id',
        });
        User.hasMany(Follow, {
            foreignKey: 'follow_id',
            as: 'followers',
            sourceKey: 'user_id',
        });
        Follow.belongsTo(User, {
            foreignKey: 'user_id',
            as: 'user',
            targetKey: 'user_id',
        });
        Follow.belongsTo(User, {
            foreignKey: 'follow_id',
            as: 'followedUser',
            targetKey: 'user_id',
        });

        // User Conversation (用户会话关系)
        User.hasMany(Conversation, {
            foreignKey: 'user1_id',
            as: 'conversationsAsUser1',
            sourceKey: 'user_id',
        });
        User.hasMany(Conversation, {
            foreignKey: 'user2_id',
            as: 'conversationsAsUser2',
            sourceKey: 'user_id',
        });
        Conversation.belongsTo(User, {
            foreignKey: 'user1_id',
            as: 'user1',
            targetKey: 'user_id',
        });
        Conversation.belongsTo(User, {
            foreignKey: 'user2_id',
            as: 'user2',
            targetKey: 'user_id',
        });

        // User Post (用户动态关系)
        User.hasMany(Post, {
            foreignKey: 'user_id',
            as: 'posts',
            sourceKey: 'user_id',
        });
        Post.belongsTo(User, {
            foreignKey: 'user_id',
            as: 'author',
            targetKey: 'user_id',
        });

        // Post Comment (动态评论关系)
        Post.hasMany(Comment, {
            foreignKey: 'post_id',
            as: 'comments',
            sourceKey: 'post_id',
        });
        Comment.belongsTo(Post, {
            foreignKey: 'post_id',
            as: 'post',
            targetKey: 'post_id',
        });

        // User Comment (用户评论关系)
        User.hasMany(Comment, {
            foreignKey: 'user_id',
            as: 'comments',
            sourceKey: 'user_id',
        });
        Comment.belongsTo(User, {
            foreignKey: 'user_id',
            as: 'commenter',
            targetKey: 'user_id',
        });

        // Comment Comment (评论回复关系)
        Comment.hasMany(Comment, {
            foreignKey: 'parent_id',
            as: 'replies',
            sourceKey: 'comment_id',
        });
        Comment.belongsTo(Comment, {
            foreignKey: 'parent_id',
            as: 'parentComment',
            targetKey: 'comment_id',
        });

        // User UserCircle (用户圈子关系)
        User.hasMany(UserCircle, {
            foreignKey: 'user_id',
            as: 'joinedCircles',
            sourceKey: 'user_id',
        });
        UserCircle.belongsTo(User, {
            foreignKey: 'user_id',
            as: 'user',
            targetKey: 'user_id',
        });

        // Circle UserCircle (圈子用户关系)
        Circle.hasMany(UserCircle, {
            foreignKey: 'circle_id',
            as: 'circleMembers',
            sourceKey: 'circle_id',
        });
        UserCircle.belongsTo(Circle, {
            foreignKey: 'circle_id',
            as: 'circle',
            targetKey: 'circle_id',
        });

        // Circle CirclePost (圈子帖子关系)
        Circle.hasMany(CirclePost, {
            foreignKey: 'circle_id',
            as: 'circlePosts',
            sourceKey: 'circle_id',
        });
        CirclePost.belongsTo(Circle, {
            foreignKey: 'circle_id',
            as: 'circle',
            targetKey: 'circle_id',
        });

        // User CirclePost (用户圈子帖子关系)
        User.hasMany(CirclePost, {
            foreignKey: 'user_id',
            as: 'circlePosts',
            sourceKey: 'user_id',
        });
        CirclePost.belongsTo(User, {
            foreignKey: 'user_id',
            as: 'author',
            targetKey: 'user_id',
        });

        // CirclePost CirclePostComment (圈子帖子评论关系)
        CirclePost.hasMany(CirclePostComment, {
            foreignKey: 'circle_post_id',
            as: 'comments',
            sourceKey: 'post_id',
        });
        CirclePostComment.belongsTo(CirclePost, {
            foreignKey: 'circle_post_id',
            as: 'circlePost',
            targetKey: 'post_id',
        });

        // User CirclePostComment (用户圈子评论关系)
        User.hasMany(CirclePostComment, {
            foreignKey: 'user_id',
            as: 'circlePostComments',
            sourceKey: 'user_id',
        });
        CirclePostComment.belongsTo(User, {
            foreignKey: 'user_id',
            as: 'commenter',
            targetKey: 'user_id',
        });

        // User Notification (用户通知关系)
        User.hasMany(Notification, {
            foreignKey: 'user_id',
            as: 'notifications',
            sourceKey: 'user_id',
        });
        Notification.belongsTo(User, {
            foreignKey: 'user_id',
            as: 'receiver',
            targetKey: 'user_id',
        });

        // User Message (用户消息关系)
        User.hasMany(Message, {
            foreignKey: 'sender_id',
            as: 'sentMessages',
            sourceKey: 'user_id',
        });
        User.hasMany(Message, {
            foreignKey: 'receiver_id',
            as: 'receivedMessages',
            sourceKey: 'user_id',
        });
        Message.belongsTo(User, {
            foreignKey: 'sender_id',
            as: 'sender',
            targetKey: 'user_id',
        });
        Message.belongsTo(User, {
            foreignKey: 'receiver_id',
            as: 'receiver',
            targetKey: 'user_id',
        });

        // User Job (用户发布职位关系)
        User.hasMany(Job, {
            foreignKey: 'publisher_id',
            as: 'publishedJobs',
            sourceKey: 'user_id',
        });
        Job.belongsTo(User, {
            foreignKey: 'publisher_id',
            as: 'publisher',
            targetKey: 'user_id',
        });

        // User Resume (用户简历关系)
        User.hasMany(Resume, {
            foreignKey: 'user_id',
            as: 'resumes',
            sourceKey: 'user_id',
        });
        Resume.belongsTo(User, {
            foreignKey: 'user_id',
            as: 'user',
            targetKey: 'user_id',
        });

        // Job JobApplication (职位申请关系)
        Job.hasMany(JobApplication, {
            foreignKey: 'job_id',
            as: 'applications',
            sourceKey: 'job_id',
        });
        JobApplication.belongsTo(Job, {
            foreignKey: 'job_id',
            as: 'job',
            targetKey: 'job_id',
        });

        // User JobApplication (用户申请关系)
        User.hasMany(JobApplication, {
            foreignKey: 'user_id',
            as: 'jobApplications',
            sourceKey: 'user_id',
        });
        JobApplication.belongsTo(User, {
            foreignKey: 'user_id',
            as: 'applicant',
            targetKey: 'user_id',
        });

        // Resume JobApplication (简历申请关系)
        Resume.hasMany(JobApplication, {
            foreignKey: 'resume_id',
            as: 'jobApplications',
            sourceKey: 'resume_id',
        });
        JobApplication.belongsTo(Resume, {
            foreignKey: 'resume_id',
            as: 'resume',
            targetKey: 'resume_id',
        });

        // User LostAndFound (用户失物招领关系)
        User.hasMany(LostAndFound, {
            foreignKey: 'user_id',
            as: 'lostAndFoundItems',
            sourceKey: 'user_id',
        });
        LostAndFound.belongsTo(User, {
            foreignKey: 'user_id',
            as: 'publisher',
            targetKey: 'user_id',
        });

        // User Good (用户二手商品关系)
        User.hasMany(Good, {
            foreignKey: 'user_id',
            as: 'goods',
            sourceKey: 'user_id',
        });
        Good.belongsTo(User, {
            foreignKey: 'user_id',
            as: 'seller',
            targetKey: 'user_id',
        });

        // Good Order (商品订单关系)
        Good.hasMany(Order, {
            foreignKey: 'goods_id',
            as: 'orders',
            sourceKey: 'item_id',
        });
        Order.belongsTo(Good, {
            foreignKey: 'goods_id',
            as: 'good',
            targetKey: 'item_id',
        });

        // User Order (用户订单关系 - 买家)
        User.hasMany(Order, {
            foreignKey: 'buyer_id',
            as: 'buyerOrders',
            sourceKey: 'user_id',
        });
        Order.belongsTo(User, {
            foreignKey: 'buyer_id',
            as: 'buyer',
            targetKey: 'user_id',
        });

        // User Order (用户订单关系 - 卖家)
        User.hasMany(Order, {
            foreignKey: 'seller_id',
            as: 'sellerOrders',
            sourceKey: 'user_id',
        });
        Order.belongsTo(User, {
            foreignKey: 'seller_id',
            as: 'seller',
            targetKey: 'user_id',
        });

        // Admin Announcement (管理员公告关系)
        Admin.hasMany(Announcement, {
            foreignKey: 'admin_id',
            as: 'announcements',
            sourceKey: 'admin_id',
        });
        Announcement.belongsTo(Admin, {
            foreignKey: 'admin_id',
            as: 'admin',
            targetKey: 'admin_id',
        });
    }
}

module.exports = AppBootHook;